#include "main.h"

enum GUESS_RESULT {
	ERROR_LARGER	=	-1,	// report when the current guess is larger (than answer)
	ERROR_CORRECT	=	0,	// report when the current guess is correct
	ERROR_SMALLER	=	1	// report when the current guess is smaller (than answer)
};

static uint32 computerRoll = 0;

//
// Returns various results based on how guess compares with the given answer.
// See the enumeration GUESS_RESULT.
//

static GUESS_RESULT const __fastcall CheckAnswer( uint32 const guess ) {
	GUESS_RESULT res = ERROR_CORRECT;

	if ( guess > computerRoll ) {
		res = ERROR_LARGER;
	} else if ( guess < computerRoll ) {
		res = ERROR_SMALLER;
	}

	return res;
}

static GUESS_RESULT const __fastcall CheckAnswerProxy( uint32 const guess ) {
	return CheckAnswer( guess );
}

//
// Returns a user's guess.
//

uint32 const GetGuess( void ) {
	uint32 guess;
	std::cin >> guess;
	return guess;
}

int main( size_t const argc, char const * const argv ) {
	if ( argc == 2 ) {
		printf( "%08X\n", CheckAnswer );
	}

	srand( time(NULL) );
	std::cout << "guess 0 to quit" << std::endl;
	computerRoll = (rand() % 100) + 1;
	std::cout << "generated a number between 1 and 100" << std::endl;
	uint32 guess = GetGuess( );
	while ( guess != 0 ) {
		switch ( CheckAnswerProxy( guess ) ) {
		case ERROR_CORRECT:
			std::cout << "correct; generating new number" << std::endl;
			computerRoll = (rand() % 100) + 1;
			break;
		case ERROR_SMALLER:
			std::cout << "too small" << std::endl;
			break;
		case ERROR_LARGER:
			std::cout << "too large" << std::endl;
			break;
		}

		guess = GetGuess( );
	}

	return 0;
}